package com.incms.web.modules.system.controller;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.incms.web.common.controller.BaseController;
import com.incms.web.common.util.Page;
import com.incms.web.modules.system.entity.SysRole;
import com.incms.web.modules.system.entity.SysUser;
import com.incms.web.modules.system.service.SysRoleService;
import com.incms.web.modules.system.service.SysUserService;
/**
 * 项目名称：inCms    
 * 类名称：SysUserController    
 * 类描述：管理员控制层
 * 创建人：liujunqing    
 * 创建时间：2015年10月15日  
 * @version 1.0    
 *
 */
@Controller
@RequestMapping("/admin/sysuser")
@Slf4j
public class SysUserController extends BaseController {
	@Resource(name = "sysUserServcie")
	private SysUserService sysUserService;
	@Resource(name = "sysRoleService")
	private SysRoleService sysRoleService;
	
	
	@RequestMapping(value="/list")
	public String list(Page<SysUser> page, SysUser sysUser,ModelMap model){
		Page<SysUser> findPage = sysUserService.querySysUserPage(sysUser, page);
		model.put("page", findPage);
		return "/admin/sys/user/user_list";
	}
	
	@RequestMapping(value="checkLoginName",method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public Boolean checkLoginName(String userName){
		SysUser sysUser = sysUserService.findByUsername(userName.toLowerCase().trim());
		return sysUser== null ? true : false;
	}
	
	@RequestMapping(value = "add")
	public String add(ModelMap modeMap){
		List<SysRole> sysRoles = sysRoleService.findList(new SysRole());
		modeMap.addAttribute("sysRoleList",sysRoles);
		return "/admin/sys/user/user_add";
	}
	
	@RequestMapping(value = "edit")
	public String edit(Long userId,ModelMap modeMap){
		SysUser sysUser = sysUserService.findById(userId);
		modeMap.addAttribute("sysUser", sysUser);
		return "/admin/sys/user/user_edit";
	}
	
	@RequestMapping(value = "save")
	public String save(SysUser sysUser,RedirectAttributes redirectAttributes){
		try {
			sysUser.setPassword(DigestUtils.md5Hex(sysUser.getPassword()));
			sysUserService.save(sysUser);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "用户操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value = "update")
	public String update(SysUser sysUser,RedirectAttributes redirectAttributes){
		try {
			sysUserService.update(sysUser);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "用户操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value = "delete")
	public String delete(Long userId,RedirectAttributes redirectAttributes){
		try {
			sysUserService.delete(userId);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
}
